package com.ahnchan.ContactsPlus;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.graphics.Color;
import android.util.Log;
import android.widget.Toast;

/**
 * Configuration information
 * 
 * @author Ahnchan
 *
 */
public class ContactsPlusSetting {

	static private ContactsPlusSetting _instance = null;

	public static final String TAB_CONTACT = "0";
	public static final String TAB_GROUP = "1";
	public static final String TAB_COMPANY = "2";
	public static final String TAB_FAVORITES = "3";

	// Search
	public static final String SHOW_SEARCH = "SHOW_SEARCH"; 

	public static final String SHARED_PREF_NAME = "com.ahnchan.contactsplus";
	
	private Properties setting = new Properties();
	private static final String SET_FILE = "/data/data/com.ahnchan.ContactsPlus/ContactsPlus.properties"; 
	
	private static final String FONT_SIZE = "FONT_SIZE";
	private static final String FONT_SIZE_INDEX = "FONT_SIZE_INDEX";
	private static final String FIRST_TAB = "FIRST_TAB";
	private static final String LANGUAGE = "LANGUAGE";
	
	private static final String INDEX_COLOR = "INDEX_COLOR";
	private static final String INDEX_BG_COLOR = "INDEX_BG_COLOR";
	private static final String FONT_COLOR = "FONT_COLOR";
	private static final String FONT_BG_COLOR = "FONT_BG_COLOR";
	private static final String TAB_COLOR = "TAB_COLOR";

	// Display/Sort option
	private static final String DISPLAY_ONLY_PHONE = "DISPLAY_ONLY_PHONE";
	private static final String SORT_LASTNAME = "SORT_LASTNAME";
	private static final String DISPLAY_LASTNAME = "DISPLAY_LASTNAME";
	
	private static final String CONTACT_INDEX = "CONTACT_INDEX";
	private static final String GROUP_INDEX = "GROUP_INDEX";
	private static final String COMPANY_INDEX = "COMPANY_INDEX";

	// Count view option
	private static final String GROUP_COUNT = "GROUP_COUNT";
	private static final String COMPANY_COUNT = "COMPANY_COUNT";
	private static final String COUNTRY = "COUNTRY"; 

	private static final String SIGN_FIRST = "SIGN_FIRST";
	private static final String LOCAL_LANGUAGE = "LOCAL_LANGUAGE";
	
	// List number
	private static final String TYPE_LIST = "TYPE_LIST";
	private static final String TYPE_SYMBOL_NUMBER = "TYPE_SYMBOL_NUMBER";
	
	private static final String SPREAD_SYMBOL = "SPREAD_SYMBOL";
	private static final String SPREAD_NUMBER = "SPREAD_NUMBER";
	private static final String SPREAD_ALPHABET = "SPREAD_ALPHABET";
	
	// Template Data like position
	private int iContactsPosition = 0;	
	private int iCompanyPosition = 0;
	private int iGroupPosition = 0;
	private int iFavoritesPosition = 0;
	
	// Search option
	private boolean bShowSearch = true;
	
	// Properties 
	private String strFontSize = "16";
	private String strFirstTab = "0";
	private String strLanguage = "";
	private String strLocalLanguage = "";
	private String strCountry = "";
	
	private boolean bSignFirst = false;
	
	// Display/Sort option
	private boolean bOnlyPhone = false;
	private boolean bSortLastname = false;
	private boolean bDisplayLastname = false;
	
	// Index option
	private boolean bContactIndex = true;
	private boolean bGroupIndex = true;
	private boolean bCompanyIndex = true;
	
	// Count option (Group, Company)
	private boolean bGroupCount = true;
	private boolean bCompanyCount = true;
	
	private int iTypeList = 1;	// 1:Alphabet+Number+Symbol, 2:Symbol+Number+Alphabet, 3:Number+Symbol+Alphabet
	private boolean bTypeSymbolNumber = true;	// true:Merge Symbol & Number
	
	private boolean bSpreadSymbol = true;
	private boolean bSpreadNumber = true;
	private boolean bSpreadAlphabet = true;

	// Index text properties
//	private int iIndexTitleSize = 20;
	private String strIndexFontSize = "20";
	private int iIndexTitleColor = Color.WHITE;
	private int iIndexTitleBGColor = 0xff3399cc;
	
	private int iFontColor = Color.WHITE;
	private int iFontBGColor = Color.BLACK;
	
	private int iTabColor = 0;
	
	private String strDetailFontSize = "20";
	
	private static final int iMinFontSize = 10; 
	
	protected ContactsPlusSetting() {
		loadSetting();
	}

	static public ContactsPlusSetting instatnce() {
		if (null == _instance) {
			_instance = new ContactsPlusSetting();
		}
		
		return _instance;
	}
	
	public int getContactsPosition() {
		return iContactsPosition;
	}
	
	public void setContactsPosition(int position) {
		iContactsPosition = position;
	}
	
	public int getCompanyPosition() {
		return iCompanyPosition;
	}
	
	public void setCompanyPosition(int position) {
		iCompanyPosition = position;
	}

	public int getGroupPosition() {
		return iGroupPosition;
	}
	
	public void setGroupPosition(int position) {
		iGroupPosition = position;
	}
	
	public int getFavoritesPosition() {
		return iFavoritesPosition;
	}
	
	public void setFavoritesPosition(int position) {
		iFavoritesPosition = position;
	}

	public String getFontSize() {
		return strFontSize;
	}
	
	public int getIntFontSize() {
		
		int iSize = 20;
		
		try {
			iSize = Integer.parseInt(strFontSize);
			
		} catch (Exception ex) {
			// return default size
		}
		
		return iSize;
	}

	public void setFontSize(String fontSize) {
		strFontSize = fontSize;
		storeSetting();
	}

	public int getMinFontSize() {
		return iMinFontSize;
	}
	
	public String getFirstTab() {
		return strFirstTab;
	}

	public void setFirstTab(String firstTab) {
		strFirstTab = firstTab;
		storeSetting();
	}

	public String getLanguage() {
		return strLanguage;
	}
	
	public void setLanguage(String language) {
		strLanguage = language;
		storeSetting();
	}

	public String getLocalLanguage() {
		return strLocalLanguage;
	}
	
	public void setLocalLanguage(String language) {
		strLocalLanguage = language;
		storeSetting();
	}

	public String getCountry() {
		return strCountry;
	}
	
	public void setCountry(String country) {
		strCountry = country;
		storeSetting();
	}

	// Search text option
	public boolean getShowSearch() {
		return bShowSearch;
	}
	
	public void setShowSearch(boolean show) {
		bShowSearch = show;
		storeSetting();
	}

	// Show only phone number existed
	public boolean getOnlyPhone() {
		return bOnlyPhone;
	}
	
	public void setOnlyPhone(boolean show) {
		bOnlyPhone = show;
		storeSetting();
	}

	public boolean getSortLastname() {
		return bSortLastname;
	}
	
	public void setSortLastname(boolean sort) {
		bSortLastname = sort;
		storeSetting();
	}

	public boolean getDisplayLastname() {
		return bDisplayLastname;
	}
	
	public void setDisplayLastname(boolean display) {
		bDisplayLastname = display;
		storeSetting();
	}

	public boolean getContactIndex() {
		return bContactIndex;
	}
	
	public void setContactIndex(boolean index) {
		bContactIndex = index;
		storeSetting();
	}

	public boolean getGroupIndex() {
		return bGroupIndex;
	}

	public void setGroupIndex(boolean index) {
		bGroupIndex = index;
		storeSetting();
	}

	public boolean getCompanyIndex() {
		return bCompanyIndex;
	}

	public void setCompanyIndex(boolean index) {
		bCompanyIndex = index;
		storeSetting();
	}

	public boolean getGroupCount() {
		return bGroupCount;
	}

	public void setGroupCount(boolean visible) {
		bGroupCount = visible;
		storeSetting();
	}

	public boolean getCompanyCount() {
		return bCompanyCount;
	}

	public void setCompanyCount(boolean visible) {
		bCompanyCount = visible;
		storeSetting();
	}

	public int getIndexFontSize() {
		
		int iSize = 20;
		
		try {
			iSize = Integer.parseInt(strIndexFontSize);
			
		} catch (Exception ex) {
			// return default size
		}
		
		return iSize;
	}

	public void setIndexFontSize(String fontSize) {
		strIndexFontSize = fontSize;
		storeSetting();
	}

	public int getDetailFontSize() {
		
		int iSize = 20;
		
		try {
			iSize = Integer.parseInt(strDetailFontSize);
			
		} catch (Exception ex) {
			// return default size
		}
		
		return iSize;
	}

	public void setDetailFontSize(String fontSize) {
		strDetailFontSize = fontSize;
		storeSetting();
	}

	public int getIndexTitleColor() {
		return iIndexTitleColor;
	}
	
	public void setIndexTitleColor(int color) {
		iIndexTitleColor = color;
		storeSetting();
	}
	
	public int getIndexTitleBGColor() {
		return iIndexTitleBGColor;
	}
	
	public void setIndexTitleBGColor(int color) {
		iIndexTitleBGColor = color;
		storeSetting();
	}

	public int getFontColor() {
		return iFontColor;
	}
	
	public void setFontColor(int color) {
		iFontColor = color;
		storeSetting();
	}
	
	public int getFontBGColor() {
		return iFontBGColor;
	}
	
	public void setFontBGColor(int color) {
		iFontBGColor = color;
		storeSetting();
	}
	
	public int getTabColor() {
		return iTabColor;
	}
	
	public void setTabColor(int color) {
		iTabColor = color;
		storeSetting();
	}
	
	public boolean getSignFist() {
		return bSignFirst;
	}
	
	public void setSignFirst(boolean signFirst) {
		bSignFirst = signFirst;
		storeSetting();
	}
	
	public String getSortType() {
		return strLanguage;
	}
	
	public void setSortType(String language) {
		strLanguage = language;
		storeSetting();
	}

	public int getTypeList() {
		return iTypeList;
	}
	
	public void setTypeList(int type) {
		iTypeList = type;
		storeSetting();
	}

	public boolean getTypeSymbolNumber() {
		return bTypeSymbolNumber;
	}
	
	public void setTypeSymbolNumber(boolean type) {
		bTypeSymbolNumber = type;
		storeSetting();
	}

	public boolean getSpreadSymbol() {
		return bSpreadSymbol;
	}
	
	public void setSpreadSymbol(boolean spread) {
		bSpreadSymbol = spread;
		storeSetting();
	}
	
	public boolean getSpreadNumber() {
		return bSpreadNumber;
	}
	
	public void setSpreadNumber(boolean spread) {
		bSpreadNumber = spread;
		storeSetting();
	}
	
	public boolean getSpreadAlphabet() {
		return bSpreadAlphabet;
	}
	
	public void setSpreadAlphabet(boolean spread) {
		bSpreadSymbol = spread;
		storeSetting();
	}
	
	/**
	 * Save setting information
	 */
	private void storeSetting() {
		
    	try {
    		setting.setProperty(SHOW_SEARCH, ""+bShowSearch);
    		
    		setting.setProperty(FONT_SIZE, strFontSize);
    		setting.setProperty(FONT_SIZE_INDEX, strIndexFontSize);

    		setting.setProperty(FIRST_TAB, strFirstTab);
    		setting.setProperty(LANGUAGE, strLanguage);	// Disable

    		setting.setProperty(DISPLAY_ONLY_PHONE, ""+bOnlyPhone);
    		setting.setProperty(SORT_LASTNAME, ""+bSortLastname);
    		setting.setProperty(DISPLAY_LASTNAME, ""+bDisplayLastname);
    		
    		setting.setProperty(CONTACT_INDEX, ""+bContactIndex);
       		setting.setProperty(GROUP_INDEX, ""+bGroupIndex);
       		setting.setProperty(COMPANY_INDEX, ""+bCompanyIndex);
       		setting.setProperty(SIGN_FIRST, ""+ bSignFirst);
       		setting.setProperty(LOCAL_LANGUAGE, strLocalLanguage);
       		setting.setProperty(COUNTRY, strCountry);
       		
       		setting.setProperty(GROUP_COUNT, ""+bGroupCount);
       		setting.setProperty(COMPANY_COUNT, ""+bCompanyCount);

       		setting.setProperty(TYPE_LIST, ""+ iTypeList);
       		setting.setProperty(TYPE_SYMBOL_NUMBER, ""+ bTypeSymbolNumber);
       		setting.setProperty(SPREAD_SYMBOL, ""+bSpreadSymbol);
       		setting.setProperty(SPREAD_NUMBER, ""+bSpreadNumber);
       		setting.setProperty(SPREAD_ALPHABET, ""+ bSpreadAlphabet);
       		
       		setting.setProperty(INDEX_BG_COLOR, ""+iIndexTitleBGColor);
       		setting.setProperty(INDEX_COLOR, ""+iIndexTitleColor);
       		
       		setting.setProperty(FONT_BG_COLOR, ""+iFontBGColor);
       		setting.setProperty(FONT_COLOR, ""+iFontColor);
       		setting.setProperty(TAB_COLOR, ""+iTabColor);
       		
       		setting.store(new FileOutputStream(SET_FILE), null);       		
    		
    	} catch (IOException ex) {
    		
    		Log.e("ContactPlusSetting", "Exception : file write exception. ", ex);
    		    		
    	}
    }
    
	/**
	 * load setting information (properties)
	 */
    public void loadSetting() {
 
    	try {   
    		// Temp data
    		iContactsPosition = 0;
    		iCompanyPosition = 0;
    		iGroupPosition = 0;
    		iFavoritesPosition = 0;
    		
    		// Properties
    		setting.load(new FileInputStream(SET_FILE));
    		
    		strFontSize = setting.getProperty(FONT_SIZE);
    		strIndexFontSize = setting.getProperty(FONT_SIZE_INDEX);
    		
    		strFirstTab = setting.getProperty(FIRST_TAB);
    		strLanguage = setting.getProperty(LANGUAGE);
    		strLocalLanguage = setting.getProperty(LOCAL_LANGUAGE);
    		strCountry = setting.getProperty(COUNTRY);

    		bShowSearch = true;
    		bOnlyPhone = false;
    		bSortLastname = false;
    		bDisplayLastname = false;
    		
    		bContactIndex = true;
    		bGroupIndex = true;
    		bCompanyIndex = true;

    		bGroupCount = true;
    		bCompanyCount = true;

    		bSignFirst = false;

    		iIndexTitleBGColor = Integer.parseInt(setting.getProperty(INDEX_BG_COLOR));
    		iIndexTitleColor = Integer.parseInt(setting.getProperty(INDEX_COLOR));
    		iFontBGColor = Integer.parseInt(setting.getProperty(FONT_BG_COLOR));
    		iFontColor = Integer.parseInt(setting.getProperty(FONT_COLOR));
    		iTabColor = Integer.parseInt(setting.getProperty(TAB_COLOR));
    		
    		if (null == setting.getProperty(FIRST_TAB)) {
    			strFirstTab = "0";
    		}
    		
    		if(null == setting.getProperty(FONT_SIZE)) {
    			strFontSize = "16";
    		}

    		if(null == setting.getProperty(FONT_SIZE_INDEX)) {
    			strIndexFontSize = "16";
    		}
    		
    		if (null == setting.getProperty(LANGUAGE)) {
    			strLanguage = "";
    		}

    		if (null == setting.getProperty(LOCAL_LANGUAGE)) {
    			strLocalLanguage = "";
    		}
    		
    		if (null == setting.getProperty(COUNTRY)) {
    			strCountry = "";
    		}

    		// Show search tab
    		if(null != setting.getProperty(SHOW_SEARCH)) {
    			if ("true".equals(setting.getProperty(SHOW_SEARCH))) {
    				bShowSearch = true;
    			}
    		}

    		// Display only existed phone number
    		if(null != setting.getProperty(DISPLAY_ONLY_PHONE)) {
    			if ("true".equals(setting.getProperty(DISPLAY_ONLY_PHONE))) {
    				bOnlyPhone = true;
    			}
    		}

    		// Sort by last name
    		if(null != setting.getProperty(SORT_LASTNAME)) {
    			if ("true".equals(setting.getProperty(SORT_LASTNAME))) {
    				bSortLastname = true;
    			}
    		}

    		// View last name first
    		if(null != setting.getProperty(DISPLAY_LASTNAME)) {
    			if ("true".equals(setting.getProperty(DISPLAY_LASTNAME))) {
    				bDisplayLastname = true;
    			}
    		}

    		if(null != setting.getProperty(CONTACT_INDEX)) {
    			if ("false".equals(setting.getProperty(CONTACT_INDEX))) {
    				bContactIndex = false;
    			}
    		}
    		
    		if(null != setting.getProperty(GROUP_INDEX)) {
    			if ("false".equals(setting.getProperty(GROUP_INDEX))) {
    				bGroupIndex = false;
    			}
    		}

    		if(null != setting.getProperty(COMPANY_INDEX)) {
    			if ("false".equals(setting.getProperty(COMPANY_INDEX))) {
    				bCompanyIndex = false;
    			}
    		}
    		
    		if(null != setting.getProperty(GROUP_COUNT)) {
    			if ("false".equals(setting.getProperty(GROUP_COUNT))) {
    				bGroupCount = false;
    			}
    		}

    		if(null != setting.getProperty(COMPANY_COUNT)) {
    			if ("false".equals(setting.getProperty(COMPANY_COUNT))) {
    				bCompanyCount = false;
    			}
    		}

    		if(null != setting.getProperty(TYPE_LIST)) {
    			iTypeList = Integer.parseInt(setting.getProperty(TYPE_LIST));
    		}

    		if(null != setting.getProperty(TYPE_SYMBOL_NUMBER)) {
    			if ("false".equals(setting.getProperty(TYPE_SYMBOL_NUMBER))) {
    				bTypeSymbolNumber = false;
    			}
    		}

    		if(null != setting.getProperty(SPREAD_SYMBOL)) {
    			if ("false".equals(setting.getProperty(SPREAD_SYMBOL))) {
    				bSpreadSymbol = false;
    			}
    		}

    		if(null != setting.getProperty(SPREAD_NUMBER)) {
    			if ("false".equals(setting.getProperty(SPREAD_NUMBER))) {
    				bSpreadNumber = false;
    			}
    		}

    		if(null != setting.getProperty(SPREAD_ALPHABET)) {
    			if ("false".equals(setting.getProperty(SPREAD_ALPHABET))) {
    				bSpreadAlphabet = false;
    			}
    		}

    		if(null != setting.getProperty(SIGN_FIRST)) {
    			if ("true".equals(setting.getProperty(SIGN_FIRST))) {
    				bSignFirst = true;
    			}
    		}
    		
    	} catch (Exception ex) {
    		
    		Log.e("ContatckPlus", "[ContactsPlusSetting] Property file have some problem. Restroing", ex);
    		
    		if (null == strFontSize) {
    			strFontSize = "16";
    		}
    		if (null == strIndexFontSize) {
    			strIndexFontSize = "20";
    		}
    		if (null == strFirstTab) {
    			strFirstTab = "0";
    		}
    		if (null == strLanguage) {
    			strLanguage = "";
    		}
    		if (null == strCountry) {
    			strCountry = "";
    		}
    		
    		iIndexTitleColor = Color.WHITE;
    		iIndexTitleBGColor = 0xff3399cc;
    		
    		iFontColor = Color.WHITE;
    		iFontBGColor = Color.BLACK;    		
    		iTabColor = 0;
    		
    		setting.setProperty(FONT_SIZE, strFontSize);
    		setting.setProperty(FONT_SIZE_INDEX, strIndexFontSize);
    		
    		setting.setProperty(FIRST_TAB, strFirstTab);
    		setting.setProperty(LANGUAGE, strLanguage);

    		// Show search
    		setting.setProperty(SHOW_SEARCH, ""+bShowSearch);
    		
    		// Display/Sort option
    		setting.setProperty(DISPLAY_ONLY_PHONE, ""+bOnlyPhone);
    		setting.setProperty(SORT_LASTNAME, ""+bSortLastname);
    		setting.setProperty(DISPLAY_LASTNAME, ""+bDisplayLastname);
    		
    		setting.setProperty(CONTACT_INDEX, ""+bContactIndex);
    		setting.setProperty(GROUP_INDEX, ""+bGroupIndex);
    		setting.setProperty(COMPANY_INDEX, ""+bCompanyIndex);

    		setting.setProperty(GROUP_COUNT, ""+bGroupCount);
    		setting.setProperty(COMPANY_COUNT, ""+bCompanyCount);

    		setting.setProperty(SIGN_FIRST, ""+bSignFirst);
    		setting.setProperty(LOCAL_LANGUAGE, strLocalLanguage);
    		setting.setProperty(COUNTRY, strCountry);

       		setting.setProperty(TYPE_LIST, ""+iTypeList);
       		setting.setProperty(TYPE_SYMBOL_NUMBER, ""+bTypeSymbolNumber);
       		setting.setProperty(SPREAD_SYMBOL, ""+bSpreadSymbol);
       		setting.setProperty(SPREAD_NUMBER, ""+bSpreadNumber);
       		setting.setProperty(SPREAD_ALPHABET, ""+ bSpreadAlphabet);
       		
       		setting.setProperty(INDEX_BG_COLOR, ""+iIndexTitleBGColor);
       		setting.setProperty(INDEX_COLOR, ""+iIndexTitleColor);
       		setting.setProperty(FONT_BG_COLOR, ""+iFontBGColor);
       		setting.setProperty(FONT_COLOR, ""+iFontColor);
       		setting.setProperty(TAB_COLOR, ""+iTabColor);       		

    	}

    	// Save properties
    	storeSetting();
    }	
    
    public void checkLanguage(String lang, String country) {
		strLocalLanguage = lang;
		strLanguage = lang;
		strCountry = country;

		storeSetting();
    }
    
    private void upgradeSetting() {

    }
    
    public void deleteSettingFile() {
    	File file = new File(SET_FILE);
    	file.delete();
    }
}
